package com.pda.common.pages.productbill;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.pda.common.R;
import com.pda.common.basemvp.activity.BaseActivity;
import com.pda.common.bean.BillBean;
import com.pda.common.database.BillInfo;
import com.pda.common.database.DataBaseControl;
import com.pda.common.pages.createproductbill.CreateProductBillActivity;
import com.pda.common.pages.relationscanf.RelationScanfCActivity;
import com.pda.common.scan.BroadcastConfig;
import com.pda.common.utils.PDAUtil;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ProductBillActivity extends BaseActivity<ProductBillModel, ProductBillView, ProductBillPresent> implements ProductBillView {
    private String TAH = "ProductBillActivity";
    @BindView(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tvright)
    TextView tvright;
    @BindView(R.id.ivu)
    ImageView ivu;
    @BindView(R.id.login_id)
    EditText loginId;
    @BindView(R.id.list)
    ListView list;
    @BindView(R.id.loginbt)
    Button loginbt;
    BillAdapter billAdapter;
    List<BillInfo> listdata = new ArrayList<BillInfo>();

    protected BroadcastReceiver mScanfBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(BroadcastConfig.CUSTOM_NAME)) {
                presenter.showlist(intent.getStringExtra("scannerdata"));
            }
        }
    };

    @Override
    public ProductBillModel createModel() {
        return new ProductBillModelImpl(this);
    }

    @Override
    public ProductBillView createView() {
        return this;
    }

    @Override
    public ProductBillPresent createPresenter() {
        return new ProductBillPresent();
    }

    @Override
    public void showlist(final BillBean data) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                intentToActivityWithSParameter(ProductBillActivity.this, RelationScanfCActivity.class, PDAUtil.keyBillDeatial, (Serializable)data);
                ProductBillActivity.this.finish();
            }
        });
    }

    @Override
    public void tip(final String str) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showMsg(str);
            }
        });
    }

    @Override
    protected void initdata() {
        tvTitle.setText("生产订单");
//        loginbt.setVisibility(View.GONE);
//        list.setVisibility(View.GONE);
        View headView = getLayoutInflater().inflate(R.layout.list_billhead, null);
        list.addHeaderView(headView);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0) {
                    return;
                }
                int index = position - 1;
                billAdapter.setSelectedPosition(index);
                billAdapter.notifyDataSetInvalidated();
                presenter.showlist(listdata.get(index).getBillCode());
            }
        });

//        loginId.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//            }
//
//            @Override
//            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//                if(charSequence.toString().length()!=0)
//                {
//                    presenter.showlist((charSequence.toString()));
//                }
//                else
//                {
//                    presenter.showlist();
//                }
//            }
//
//            @Override
//            public void afterTextChanged(Editable editable) {
//            }
//        });

        initBroadcastReciever();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BroadcastConfig.CUSTOM_NAME);
        registerReceiver(mScanfBroadcastReceiver, intentFilter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_productbill);
        ButterKnife.bind(this);
        initdata();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mScanfBroadcastReceiver);
    }

    @Override
    protected void onResume() {
        super.onResume();
        listdata = DataBaseControl.getBills(PDAUtil.RELATION);
        billAdapter = new BillAdapter(this, listdata);
        list.setAdapter(billAdapter);
    }

    @Override
    protected void onPause() {
        super.onPause();
    }


    @OnClick({R.id.iv_title_left, R.id.loginbt,R.id.ivu})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.loginbt:
                intentToActivityWithoutParameter(this, CreateProductBillActivity.class);
                finish();
                break;
            case R.id.ivu:
                if(loginId.getText().toString()!=null && loginId.getText().toString().length()>0)
                {
                    presenter.showlist(loginId.getText().toString());
                }
                else
                {
                    showMsg("查询请输入单号");
                }
                break;
        }
    }
}
